package ece452.ims.view;

import javax.swing.*;

import java.awt.*;
import java.awt.event.*;
import java.beans.PropertyChangeListener;
import java.io.IOException;
import javax.swing.event.*;
import java.util.*;
// import control classes
import ece452.ims.client.*;
import ece452.ims.connection.*;

public class ChatWindow extends JPanel implements ActionListener{
	
	public String uname;
	private JTextArea receive, send;
	private static Relay rl;
	private JMenuItem squelchUser;
	private JMenuItem unsquelchUser;
	
	public ChatWindow(Relay rl, String un){
		super(new GridLayout(2,1));
		this.uname = un;
		this.setName(this.uname);
		this.rl = rl;
		receive = new JTextArea(7,25);
		send = new JTextArea(6,17);
		receive.setEditable(false);
		receive.setLineWrap(true);
		send.setLineWrap(true);
		send.setEditable(true);
		
		JPanel receivePanel = new JPanel();
		receivePanel.setLayout(new FlowLayout(FlowLayout.LEFT));
		receivePanel.add(receive);
		receivePanel.setBorder(BorderFactory.createEmptyBorder(0,0,0,0));
		//receivePanel.setBorder(BorderFactory.createLineBorder(Color.BLUE));
		
		JScrollPane recScrollPanel = new JScrollPane(receivePanel);
		recScrollPanel.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
		recScrollPanel.setPreferredSize(new Dimension(250, 100));
		//recScrollPanel.setMinimumSize(new Dimension(10, 10));
		
		JButton sendButton;
		sendButton = new JButton("Send");
		sendButton.setPreferredSize(new Dimension(75,85));
		
		send.getInputMap().put(KeyStroke.getKeyStroke("ENTER"),"Send");
		send.getActionMap().put("Send", new AbstractAction(){
			public void actionPerformed(ActionEvent e){
				String getString = send.getText();
				ChatWindow.rl.sendMessage(uname,getString);
				if(getString.equals("")){
					// ...
				}else{
					send.setText("");
					updateReceive(Login.getUsername()+": "+getString);
				}
			}
		});
		//send.getActionMap().put("Send",new KeyboardInput());
		sendButton.addActionListener(this);
		
		JPanel sendPanel = new JPanel();
		sendPanel.setLayout(new FlowLayout(FlowLayout.CENTER));
		sendPanel.add(send);
		sendPanel.add(sendButton);
		//sendPanel.setBorder(BorderFactory.createEmptyBorder(0,0,0,0));
		sendPanel.setBorder(BorderFactory.createLineBorder(Color.BLACK));
		
		JScrollPane sendScrollPanel = new JScrollPane(sendPanel);
		sendScrollPanel.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
		sendScrollPanel.setPreferredSize(new Dimension(100, 100));
		sendScrollPanel.setMinimumSize(new Dimension(10, 10));
		
		//setJMenuBar(bar);
		add(recScrollPanel);
		//add(new JSeparator(SwingConstants.VERTICAL));
		add(sendScrollPanel);
	}
	
	public void actionPerformed(ActionEvent e) {
		String getString = send.getText();
		//Relay.sendMessage(uname, getString);
		rl.sendMessage(uname, getString);
		if(getString.equals("")){
			// ...
		}else{
			send.setText("");
			updateReceive(Login.getUsername()+": "+getString);
		}
	}
	
	protected void updateReceive(String msg){
		receive.setText(receive.getText()+"\n"+msg);
	}
	
	protected JMenuBar getCWMenuBar(){
		JMenuBar bar = new JMenuBar();
		JMenu file = new JMenu("File");
		JMenuItem sendFile = new JMenuItem("Send File");
		sendFile.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				System.out.println("Send File");
			}
		});
		squelchUser= new JMenuItem("Squelch");
		squelchUser.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				System.out.println("squelch: " + uname);
				rl.squelchUser(uname);
			}
		});
		unsquelchUser = new JMenuItem("Unsquelch");
		unsquelchUser.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				System.out.println("unsquelch");
				rl.unsquelchUser(uname);
			}
		});
		file.add(sendFile);
		file.add(squelchUser);
		file.add(unsquelchUser);
		bar.add(file);
		return bar;
	}
	
	/**
	 * Method to pack free-floating items into a JFrame.
	 */
	public static void showGUI() {
		JFrame frame = new JFrame("Chat Window");
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		// frame.setPreferredSize(new Dimension(320,280));
		frame.setPreferredSize(new Dimension(310, 302));
		frame.getContentPane().add(new ChatWindow(null,""), BorderLayout.CENTER);
		frame.pack();
		frame.setVisible(true);
		frame.setJMenuBar(new ChatWindow(null,null).getCWMenuBar());
		frame.setResizable(false);
	}
	
	/**
	 * Launch the GUI. =]
	 * @param args	Commandline arguments - ignored.
	 */
	public static void main(String[] args) {
		javax.swing.SwingUtilities.invokeLater(new Runnable() {
			public void run() { showGUI(); }
		});
	}
}
